﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;

namespace Project5188.BUS.Personnel
{
    public class PersonnelWorkOuts : DetailView<personnelworkout>
    {
        private project5188Entities _context;
        // ReSharper disable InconsistentNaming
        private new List<personnelworkout> EntityList;
        private new personnelworkout Entity;
        // ReSharper restore InconsistentNaming

        public long TransactionId { get; private set; }

        #region Overrides of DetailView<personnelworkout>

        public override List<personnelworkout> GetEntityList()
        {
            _context = new project5188Entities();
            EntityList = _context.personnelworkouts.Where(a => !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override List<personnelworkout> GetEntityById(long id)
        {
            _context = new project5188Entities();
            EntityList = _context.personnelworkouts.Where(a => a.a_ID == id && !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override personnelworkout GetEntity()
        {
            _context = new project5188Entities();
            Entity = new personnelworkout();
            return Entity;
        }

        public override void Update()
        {
            _context.SaveChanges();
        }

        public override void Insert()
        {
            _context.AddTopersonnelworkouts(Entity);
            _context.SaveChanges();
            TransactionId = Entity.a_ID;
        }

        #endregion

        public List<personnelworkout> GetEntityListByParameters(string registryNoText, string nameSurname, string tcNoText)
        {
            _context = new project5188Entities();
            int tcNo = 0, registryNo = 0;

            if (!String.IsNullOrEmpty(tcNoText) && !String.IsNullOrWhiteSpace(tcNoText))
            {
                Int32.TryParse(tcNoText, out tcNo);
            }

            if (!String.IsNullOrEmpty(registryNoText) && !String.IsNullOrWhiteSpace(registryNoText))
            {
                Int32.TryParse(registryNoText, out registryNo);
            }

            var objectParameters = new ObjectParameter[3];

            var registryParameter = new ObjectParameter("RegistryNo", typeof(int)) { Value = registryNo };
            objectParameters[0] = registryParameter;

            var nameSurnameParameter = new ObjectParameter("NameSurname", typeof(string)) { Value = nameSurname };
            objectParameters[1] = nameSurnameParameter;

            var tcNoParameter = new ObjectParameter("TCNo", typeof(int)) { Value = tcNo };
            objectParameters[2] = tcNoParameter;

            var list = _context.ExecuteFunction<personnelworkout>("GetPersonnelWorkOutListByParameters", objectParameters).ToList();

            return list;
        }

        public List<GetWorkoutCausesReport_Result> GetWorkoutCausesReport()
        {
            _context = new project5188Entities();

            return _context.ExecuteFunction<GetWorkoutCausesReport_Result>("GetWorkoutCausesReport").ToList();
        }

        public List<GetWorkoutStylesReport_Result> GetWorkoutStylesReport()
        {
            _context = new project5188Entities();

            return _context.ExecuteFunction<GetWorkoutStylesReport_Result>("GetWorkoutStylesReport").ToList();
        }
    }
}
